package com.circle.view;

import com.circle.model.module.ConfigData;
import com.circle.model.util.ConfigDataFactory;
import com.circle.model.util.ResourceUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


public class DocForOutFilePage extends JPanel {
    // 全局字体配置
    private static final String FONT_NAME = "微软雅黑";
    private static final int FONT_SIZE_NORMAL = 14;
    private static final Font FONT_NORMAL = new Font(FONT_NAME, Font.PLAIN, FONT_SIZE_NORMAL);
    private static final Font FONT_BOLD = new Font(FONT_NAME, Font.BOLD, FONT_SIZE_NORMAL);

    // 全局尺寸配置
    private static final int ROW_HEIGHT = 35;
    private static final int HEADER_HEIGHT = 40;
    private static final int BUTTON_HEIGHT = 35;
    private static final int TEXTFIELD_HEIGHT = 35;
    private static final Dimension SEARCH_BUTTON_SIZE = new Dimension(100, BUTTON_HEIGHT);
    private static final Dimension PAGE_BUTTON_SIZE = new Dimension(80, 30);
    private static final Insets PADDING = new Insets(8, 8, 8, 8);

    private JTextField custCodeField;
    private JTextField custNameField;
    private JTextField docTypeField;
    private JTextField docNumberField;
    private JTable documentTable;
    private JTextField exportField;
    private DefaultTableModel tableModel;
    private JLabel totalLabel = new JLabel("共 0 条记录");

    //==========================================
    //private DocTable docTable;

    private DocForOutTable docForOutTable;

    public DocForOutFilePage() {
        setLayout(new BorderLayout());

        // 创建搜索面板
        add(createSearchPanel(), BorderLayout.NORTH);

        // 创建表格
        createTable();
        JScrollPane scrollPane = new JScrollPane(documentTable);
        add(scrollPane, BorderLayout.CENTER);


    }

    private JPanel createSearchPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = PADDING;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 第一行
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.CENTER;

        gbc.gridx = 0;
        JLabel label1 = new JLabel("来源代号:");
        label1.setFont(FONT_NORMAL);
        panel.add(label1, gbc);

        gbc.gridx = 1;
        custCodeField = createStyledTextField();
        panel.add(custCodeField, gbc);

        gbc.gridx = 2;
        JLabel label2 = new JLabel("来源名称:");
        label2.setFont(FONT_NORMAL);
        panel.add(label2, gbc);

        gbc.gridx = 3;
        custNameField = createStyledTextField();
        panel.add(custNameField, gbc);

        // 导出按钮
        gbc.gridx = 4;
        JButton exportButton = new JButton("导出文件");
        exportButton.setPreferredSize(SEARCH_BUTTON_SIZE);
        exportButton.setBackground(new Color(24, 116, 205));
        exportButton.setForeground(Color.WHITE);
        exportButton.setFont(FONT_NORMAL);
        panel.add(exportButton, gbc);

        // 第二行
        gbc.gridy = 1;

        gbc.gridx = 0;
        JLabel label3 = new JLabel("文件类型:");
        label3.setFont(FONT_NORMAL);
        panel.add(label3, gbc);

        gbc.gridx = 1;
        docTypeField = createStyledTextField();
        panel.add(docTypeField, gbc);

        gbc.gridx = 2;
        JLabel label4 = new JLabel("文件编号:");
        label4.setFont(FONT_NORMAL);
        panel.add(label4, gbc);

        gbc.gridx = 3;
        docNumberField = createStyledTextField();
        panel.add(docNumberField, gbc);

        // 搜索按钮
        gbc.gridx = 4;
        JButton searchButton = new JButton("搜索");
        searchButton.setPreferredSize(SEARCH_BUTTON_SIZE);
        searchButton.setBackground(new Color(24, 116, 205));
        searchButton.setForeground(Color.WHITE);
        searchButton.setFont(FONT_NORMAL);
        panel.add(searchButton, gbc);

        // 导出目录
        gbc.gridy = 2;

        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.weightx = 0;

        JLabel exportLabel = new JLabel("导出目录：");
        exportLabel.setFont(FONT_NORMAL);
        panel.add(exportLabel, gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 3;
        gbc.weightx = 1.0;

        JPanel exporPanel = new JPanel(new BorderLayout());
        String exportPath = ResourceUtil.getDBConfig("export.Outpath");
        exportField = new JTextField(exportPath);
        exportField.setPreferredSize(new Dimension(500, TEXTFIELD_HEIGHT));
        exportField.setFont(FONT_NORMAL);
        exporPanel.add(exportField, BorderLayout.CENTER);

        JButton exportBrowseButton = new JButton("选择目录");
        exportBrowseButton.setPreferredSize(new Dimension(100, TEXTFIELD_HEIGHT));
        exportBrowseButton.setFont(FONT_NORMAL);
        exporPanel.add(exportBrowseButton, BorderLayout.EAST);
        panel.add(exporPanel, gbc);


        //查询
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                docForOutTable.search(custCodeField.getText(), custNameField.getText(), docTypeField.getText(), docNumberField.getText());
            }
        });

        //导出
        exportBrowseButton.addActionListener(e -> {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                File directory = chooser.getSelectedFile();
                exportField.setText(directory.getAbsolutePath());
            }
        });

        exportButton.addActionListener((e) -> {
            // 执行实际的导出操作
            docForOutTable.setComponent(DocForOutFilePage.this);
            ConfigData configData = ConfigDataFactory.getInstance();
            configData.setExportDir(exportField.getText());
            docForOutTable.export(custCodeField.getText(), custNameField.getText(), docTypeField.getText(), docNumberField.getText());
        });
        return panel;
    }

    private void createTable() {
        // 创建不同对齐方式的渲染器
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);

        DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
        leftRenderer.setHorizontalAlignment(JLabel.LEFT);

        String[] columnNames = {"文件编号", "文件名称", "版本", "负责人", "来源名称", "来源代号", "文件类型"};
        tableModel = new DefaultTableModel(columnNames, 0);
        documentTable = new JTable(tableModel);
        // 设置表格行高
        documentTable.setRowHeight(ROW_HEIGHT);
        // 设置每列的对齐方式
        documentTable.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);  // 文件编号居中
        documentTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
        documentTable.getColumnModel().getColumn(3).setCellRenderer(centerRenderer);
        documentTable.getColumnModel().getColumn(4).setCellRenderer(centerRenderer);
        documentTable.getColumnModel().getColumn(5).setCellRenderer(centerRenderer);
        documentTable.getColumnModel().getColumn(6).setCellRenderer(centerRenderer);

        // 调整列宽
        documentTable.getColumnModel().getColumn(1).setPreferredWidth(200); // 增加文件名称列的宽度

        // 设置表格标题栏字体
        JTableHeader header = documentTable.getTableHeader();
        header.setFont(FONT_BOLD);
        header.setPreferredSize(new Dimension(header.getWidth(), HEADER_HEIGHT));

        // 设置表格内容字体
        documentTable.setFont(FONT_NORMAL);

        // 添加示例数据
        //addSampleData();
        docForOutTable = new DocForOutTable() {
            {
                setTableModel(tableModel);
                setTotalLabel(totalLabel);
            }
        };
        //docTable.search();

        // 创建分页面板
        add(createPaginationPanel(), BorderLayout.SOUTH);
    }

    private void addSampleData() {
        Object[][] sampleData = {
                {"DOC20230601001", "HR", "人力资源部", "人事档案", "2023-06-01 09:30", "已审核"},
                {"DOC20230602002", "FIN", "财务部", "财务报表", "2023-06-02 14:20", "审核中"},
                {"DOC20230603003", "MKT", "市场部", "营销方案", "2023-06-03 11:15", "已审核"},
                {"DOC20230604004", "RD", "研发部", "技术文档", "2023-06-04 16:45", "已驳回"},
                {"DOC20230605005", "OPS", "运营部", "运营报告", "2023-06-05 10:30", "已审核"}
        };

        for (Object[] row : sampleData) {
            tableModel.addRow(row);
        }
    }

    private JPanel createPaginationPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        totalLabel.setFont(FONT_NORMAL);
        panel.add(totalLabel);
        
        /*String[] buttonTexts = {"上一页", "1", "2", "3", "24", "下一页"};
        for (String text : buttonTexts) {
            JButton button = new JButton(text);
            button.setPreferredSize(PAGE_BUTTON_SIZE);
            button.setFont(FONT_NORMAL);
            panel.add(button);
            
            if (text.equals("3")) {
                JLabel dots = new JLabel("...");
                dots.setFont(FONT_NORMAL);
                panel.add(dots);
            }
        }*/

        return panel;
    }

    private JTextField createStyledTextField() {
        JTextField textField = new JTextField(20);
        textField.setPreferredSize(new Dimension(textField.getPreferredSize().width, TEXTFIELD_HEIGHT));
        textField.setFont(FONT_NORMAL);
        return textField;
    }
} 